<?php
namespace app\app\controller\v1;
use think\Db;
use app\app\util\OnlineUtils;
class ShoppingCartController extends  BaseController{

    /**
     * @return array
     * 我的购物车
     */
    public function MyShoppingCart()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $data = Db::name('shopping_cart')->alias('s')->join('line l','s.line_id = l.id','left')
            ->field('s.*,l.*')
            ->where(['s.user_id'=>$user_id,'s.status'=>1])->select();
        return json_code('200','success',$data);
    }

    /**
     * @return array
     * 删除购物车线路
     */
    public function DelCartLine()
    {
        $user_id = OnlineUtils::getUserIdBySid();
        $line_id = input('get.line_id');
        Db::name('shopping_cart')->where(['user_id'=>$user_id,'line_id'=>$line_id])->update(['status'=>0]);
        return json_code('200','删除成功');
    }

    /**
     * @return array
     * 保存购物车数据
     */
    public function SaveShoppingCart()
    {
		$user_id = OnlineUtils::getUserIdBySid();
        $data = json_decode(input('get.data'));
        //删除原来的数据
        Db::name('shopping_cart')->where('user_id','eq',$user_id)->delete();
        //循环插入新数据
        foreach ( $data as $a => $v){
            $arr = array('line_id'=>$v->line_id,'num'=>$v->num,'user_id'=>$user_id,'created_at'=>time());
            Db::name('shopping_cart')->insertGetId($arr);
            $datas[] = $arr;
        }
        return json_code('200','success',$datas);

    }




}